﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OLTS.DAL;
using OLTS.MODEL;
namespace OLTS.DATA_SERVICE
{
    public class Education_Service
    {
        public Education_Service()
        {
        }
        public List<OTS_EDUCATION> GetAll()
        {
            using (var ctx = new Entities())
            {
                var list = ctx.OTS_EDUCATION.ToList();
                return list;
            }
        }
        public void Add(List<OTS_EDUCATION> list)
        {
            foreach (var obj in list)
            {
                if (obj.Action == ActionEnum.Create)
                {
                    var model = new OTS_EDUCATION();
                    model.EMPLOYEE_ID = obj.EMPLOYEE_ID;
                    model.UNIVERSITY = obj.UNIVERSITY;
                    model.YEAROFEDU = obj.YEAROFEDU;
                    model.MAJOR = obj.MAJOR;
                    model.CERTIFICATION = obj.CERTIFICATION;
                    using (var ctx = new Entities())
                    {
                        ctx.OTS_EDUCATION.AddObject(model);
                        ctx.SaveChanges();
                    }
                }
            }
        }

        public void Edit(List<OTS_EDUCATION> list)
        {
            foreach (var obj in list)
            {
                if (obj.Action == ActionEnum.Update)
                {
                    using (var ctx = new Entities())
                    {
                        var objEdit = ctx.OTS_EDUCATION.Where(x => x.ID == obj.ID).FirstOrDefault();
                        if (objEdit != null)
                        {
                            objEdit.UNIVERSITY =obj.UNIVERSITY;
                            objEdit.YEAROFEDU = obj.YEAROFEDU;
                            objEdit.MAJOR = obj.MAJOR;
                            objEdit.CERTIFICATION = obj.CERTIFICATION;
                            ctx.SaveChanges();
                        }
                    }
                }
            }
        }

        public List<OTS_EDUCATION> GetByCurentLogin(string empid)
        {
            using (var ctx = new Entities())
            {
                var list = ctx.OTS_EDUCATION.Where(x => x.EMPLOYEE_ID == empid).ToList();
                return list;
            }
        }
    }
}
